package com.atguigu.srb.core.service.impl;

import com.atguigu.srb.core.enums.TransTypeEnum;
import com.atguigu.srb.core.mapper.LendItemMapper;
import com.atguigu.srb.core.pojo.entity.LendItem;
import com.atguigu.srb.core.pojo.entity.TransFlow;
import com.atguigu.srb.core.mapper.TransFlowMapper;
import com.atguigu.srb.core.pojo.entity.UserBind;
import com.atguigu.srb.core.service.TransFlowService;
import com.atguigu.srb.core.service.UserBindService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Map;

/**
 * <p>
 * 交易流水表 服务实现类
 * </p>
 *
 * @author AtLong
 * @since 2022-06-11
 */
@Service
public class TransFlowServiceImpl extends ServiceImpl<TransFlowMapper, TransFlow> implements TransFlowService {
    @Resource
    private UserBindService userBindService;
    @Resource
    private LendItemMapper lendItemMapper;
    @Resource
    TransFlowMapper transFlowMapper;

    @Override
    public boolean isSaveTransFlow(String agentBillNo) {
        QueryWrapper<TransFlow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("trans_no", agentBillNo);
        int count = baseMapper.selectCount(queryWrapper);
        if (count > 0) {
            return true;
        }
        return false;
    }

    @Override
    public void saveTransFlow(Map<String, Object> paramMap) {
        String agentBillNo = (String) paramMap.get("agentBillNo");
        String voteBindCode = (String) paramMap.get("voteBindCode");
        BigDecimal voteAmt = new BigDecimal(paramMap.get("voteAmt") + "");
        String voteNote = (String) paramMap.get("voteNote");
        String agentProjectCode = (String) paramMap.get("agentProjectCode");

        QueryWrapper<UserBind> userBindQueryWrapper = new QueryWrapper<>();
        userBindQueryWrapper.eq("bind_code", voteBindCode);
        UserBind voteUserBind = userBindService.getOne(userBindQueryWrapper);
        QueryWrapper<LendItem> lendItemQueryWrapper = new QueryWrapper<>();
        lendItemQueryWrapper.eq("invest_user_id", voteUserBind.getUserId());
        LendItem lendItem = lendItemMapper.selectOne(lendItemQueryWrapper);

        TransFlow transFlow = new TransFlow();
        transFlow.setUserId(lendItem.getInvestUserId());
        transFlow.setUserName(lendItem.getInvestName());
        transFlow.setTransNo(agentBillNo);
        transFlow.setTransType(TransTypeEnum.INVEST_LOCK.getTransType());
        transFlow.setTransTypeName(TransTypeEnum.INVEST_LOCK.getTransTypeName());
        transFlow.setTransAmount(voteAmt);
        transFlow.setMemo(agentProjectCode +"-->"+ voteNote + "标定投标锁定!");
        transFlowMapper.insert(transFlow);

    }
}
